import axios from "axios";
import store from "../store";
import { Toast } from "vant";

//设置基本配置
axios.defaults.baseURL = "https://elm.cangdu.org";
axios.defaults.timeout = "4000";

//请求拦截
axios.interceptors.request.use(
	(config) => {
		//开启Loading效果
		store.commit("setLoading", true);
		return config;
	},
	(err) => {
		Toast({
			message: "接口错误",
			position: "top",
		});
		return Promise.reject(err);
	}
);

//响应拦截
axios.interceptors.response.use(
	(response) => {
		//关闭Loading效果
		store.commit("setLoading", false);
		return response.data;
	},
	(err) => {
		//关闭Loading效果
		store.commit("setLoading", false);
		Toast({
			message: "接口错误",
			position: "top",
		});
		return Promise.reject(err);
	}
);

export default axios;
